//
//  DiscoverListHeaderView.m
//  ShowYork
//
//  Created by hxh on 2021/1/18.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "DiscoverListHeaderView.h"
#import "XHPageControl.h"
#import "LessonBannerModel.h"
@interface DiscoverListHeaderView ()<SDCycleScrollViewDelegate>
@property (nonatomic,strong)SDCycleScrollView *scrollView;
@property (nonatomic,strong)XHPageControl *page;
@end

@implementation DiscoverListHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{

    
    
    self.backgroundColor = RGBA(20, 26, 40, 1);
    
    CGFloat rate = 137.0/343.0;
    
    CGFloat itemW = kScreenW - 32;
    CGFloat itemH = itemW * rate;
    
    self.scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(16,16,itemW, itemH) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    self.scrollView.autoScrollTimeInterval = 3;
    self.scrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    self.scrollView.backgroundColor = RGBA(117, 120, 127, 1);
    self.scrollView.showPageControl = NO;
    self.scrollView.layer.cornerRadius = 8;
    self.scrollView.layer.masksToBounds = YES;
    [self addSubview:self.scrollView];
    
    
    self.page = [[XHPageControl alloc]initWithFrame:CGRectMake(0,itemH - 6, kScreenW, 30)];
    self.page.numberOfPages = 5;
    self.page.currentPage = 0;
    self.page.currentMultiple = 1;
    self.page.controlSize = 4;
    self.page.currentMultiple = 2;
    self.page.controlSpacing = 2;
    self.page.currentColor = RGBA(245, 75, 100, 1);
    self.page.otherColor = RGBA(58, 64, 79, 1);
    [self addSubview:self.page];
}
- (void)setDiscoverBannerArray:(NSArray *)bannerArray{
    if (bannerArray.count == 1) {
        self.scrollView.autoScroll = NO;
    }else{
        self.scrollView.autoScroll = YES;
    }
 
    self.hidden = bannerArray.count == 0;
    
    NSMutableArray *urls = [NSMutableArray array];
    [bannerArray enumerateObjectsUsingBlock:^(LessonBannerModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [urls addObject:obj.image];
    }];
    
    self.page.numberOfPages = bannerArray.count;
    
    self.page.hidden = bannerArray.count <= 1;
    
    self.scrollView.imageURLStringsGroup = urls;
}
- (void)adjustWhenControllerViewWillAppera{
    [self.scrollView adjustWhenControllerViewWillAppera];
}

/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    if (self.didSelected) {
        self.didSelected(index);
    }
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    self.page.currentPage = index;
}

@end
